﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameBase;

namespace _0912089
{
    public abstract class ActionHelper : InvisibleGameEntity, IPublisher, ITopic
    {
        protected View _view;

        public virtual View View
        {
            get { return _view; }
            set { _view = value; }
        }
        protected Vector2 _endPosition;
        protected Vector2 _currentPosition;
        protected Vector2 _beginPosition;

        protected bool _isActionDone = false;
        protected bool _isActioning = false;

        protected string _actionName;

        public string ActionName
        {
            get { return _actionName; }
        }

        public Vector2 BeginPosition
        {
            get { return _beginPosition; }
            set { _beginPosition = value; }
        }

        public Vector2 CurrentPosition
        {
            get { return _currentPosition; }
            set { _currentPosition = value; }
        }

        public Vector2 EndPosition
        {
            get { return _endPosition; }
            set { _endPosition = value; }
        }

        public ActionHelper() { }

        public ActionHelper(string topicName)
        {
            NotificationCenter.addNewTopic(this);
        }

        public virtual void reverse()
        {
            _isActioning = false;
            _isActionDone = false;
            _currentTotalTime = 0;
        }

        public void start()
        {
            _isActioning = true;
        }

        public override void update(GameTime gameTime)
        {
            if (_isActionDone || !_isActioning)
                return;
            excuteAction(gameTime);
        }

        public virtual void excuteAction(GameTime gameTime)
        {

        }

        #region IPublisher 
        public void postNotification(string topicName, string message)
        {
            NotificationCenter.postNotification(topicName, message);
        }
        #endregion

        #region ITopic 
        private List<ISubscriber> _subscribers = new List<ISubscriber>();

        public string TopicName
        {
            get { return _actionName; }
        }

        public void addSubscriber(ISubscriber subscriber)
        {
            _subscribers.Add(subscriber);
        }

        public void postNotification(string message)
        {
            foreach (ISubscriber subscriber in _subscribers)
            {
                if (!subscriber.notify(message))
                    break;
            }
        }
        #endregion
    }
}
